<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/9/15 15:01
 * Desc: 发票验证
 */
class Request_Api_V2_Member_Invoice extends Request_Api_V2_Base
{
    public function __construct($request_param)
    {
        parent::__construct($request_param);
    }

    public function list()
    {
        $type = array(
            Model_Api_V2_Member_Invoice::TYPE_PERSONAL,
            Model_Api_V2_Member_Invoice::TYPE_COMPANY,
            Model_Api_V2_Member_Invoice::TYPE_VAT
        );

        $this->rules = [
            ['pagesize', ['digit', $this->rule_min(1)]],
            ['page', ['digit', $this->rule_min(0)]],
        ];

        //自定义规则
        $this->validation->rule('mid', array('Rule_Member', 'need_member'));

        return $this;
    }

    public function show_info()
    {
        $this->rules = [
            ['id', ['not_empty', 'digit', $this->rule_min(1)]],
        ];
        //自定义规则
        $this->validation->rule('mid', array('Rule_Member', 'need_member'));

        return $this;
    }

    public function delete()
    {
        $this->rules = [
            ['id', ['not_empty', 'digit', $this->rule_min(1)]],
        ];

        //自定义规则
        $this->validation->rule('mid', array('Rule_Member', 'need_member'));

        return $this;
    }

    public function insert()
    {

        $this->rules = $this->edit_rule();

        //自定义规则
        $this->validation->rule('mid', array('Rule_Member', 'need_member'));

        return $this;
    }

    public function update()
    {
        $rules       = $this->edit_rule();
        $rules[]     = ['id', ['not_empty', 'digit', $this->rule_min(1)]];
        $this->rules = $rules;

        //自定义规则
        $this->validation->rule('mid', array('Rule_Member', 'need_member'));

        return $this;
    }

    /**
     * 添加或者更新的验证条件
     * @return array[]
     */
    private function edit_rule()
    {
        $type = array(
            Model_Api_V2_Member_Invoice::TYPE_PERSONAL,
            Model_Api_V2_Member_Invoice::TYPE_COMPANY,
            Model_Api_V2_Member_Invoice::TYPE_VAT
        );

        $rules = [
            ['type', ['digit', $this->rule_in_array($type)]],
        ];

        $type = $this->request_param['type'];
        // 公司类型需要纳税人识别号
        if (in_array($type, array(Model_Api_V2_Member_Invoice::TYPE_COMPANY, Model_Api_V2_Member_Invoice::TYPE_VAT))) {
            $rules[] = ['taxpayer_number', ['not_empty']];
        }

        // 增值税类型需要纳税人银行信息
        if ($type == Model_Api_V2_Member_Invoice::TYPE_VAT) {
            $rules[] = ['taxpayer_address', ['not_empty']];
            $rules[] = ['taxpayer_phone', ['not_empty', 'phone']];
            $rules[] = ['bank_branch', ['not_empty']];
            $rules[] = ['bank_account', ['not_empty']];
        }

        return $rules;
    }

    public function get_order_invoice()
    {
        $this->rules = [
            ['type_id', ['not_empty', 'digit', $this->rule_min(1)]],
        ];

        return $this;
    }
}